#include "usb485.h"

void Usb485::run()
{
    serial = new QSerialPort(this);
    if(portName.length()==0)
        portName = "/dev/ttyUSB0";
    while(1)
    {
        if(initFlag == 1)
        {
            initFlag = 0;
            runFlag = 1;
            counter = 0;

            serial->setPortName(portName);
            serial->open(QIODevice::ReadWrite);

            if(serial->isOpen())
            {
                    qDebug()<<"port opened";
                    serial->setBaudRate(2000000);
                    serial->setDataBits(QSerialPort::Data8);
                    serial->setParity(QSerialPort::NoParity);
                    serial->setStopBits(QSerialPort::OneStop);
                    serial->setFlowControl(QSerialPort::NoFlowControl);
                    serial->setReadBufferSize(32);

                    while(runFlag)
                    {
                        serialTxBuf[0] = 0x20;
                        serial->write((char *)serialTxBuf,32);
                        serialRxBuf[0] = 0x0;
                        serial->waitForReadyRead(-1);
                        serial->read((char *)serialRxBuf, 32);

                        if(serialRxBuf[0] > 0)
                        {
                            counter++;
                            serialTxBuf[0] = 0x0;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)adcFrameData,32);

                            serialTxBuf[0] = 0x1;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+32),32);

                            serialTxBuf[0] = 0x2;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+64),32);

                            serialTxBuf[0] = 0x3;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+96),32);


                            serialTxBuf[0] = 0x4;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+32*4),32);

                            serialTxBuf[0] = 0x5;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+32*5),32);

                            serialTxBuf[0] = 0x6;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+32*6),32);

                            serialTxBuf[0] = 0x7;
                            serial->write((char *)serialTxBuf,32);
                            serial->waitForReadyRead(-1);
                            serial->read((char *)(adcFrameData+32*7),32);

                            plotx.append(plotTimeline);
                            plotTimeline++;
                            tempIndex = plotChannel;
                            if(tempIndex > 127)
                            {
                                tempIndex = 127;
                            }
                            if(tempIndex<0)
                            {
                                tempIndex = 0;
                            }
                            ploty.append(*(short *)(&(adcFrameData[2*plotChannel]))/8);
                            if(plotx.size() > 500)
                            {
                                plotx.pop_front();
                                ploty.pop_front();
                            }
                        }
                        else
                        {
                            msleep(5);
                        }
                        serial->clear();
                    }
                    serial->close();
            }
        }
        else
        {
            msleep(20);
        }
    }
}

//void Usb485::setPortName(QString serialportName)
//{
//    portName = serialportName;
//}

void Usb485::init(QString serialportName)
{
    portName = serialportName;
    initFlag = 1;
}

void Usb485::stop()
{
    runFlag = 0;
}
